﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class LoaiSanPhamDAL
    {
        public static List<LoaiSanPhamBUS> GetAll()
        {
            ShopDataContext db = DataAccess.Database;
            var list = from p in db.LoaiSanPhams
                       select new LoaiSanPhamBUS()
                       {
                           ID = p.ID,
                           MaLoai = p.MaLoai,
                           TenLoai = p.TenLoai
                       };
            return list.ToList<LoaiSanPhamBUS>();
        }

        internal static int Insert(LoaiSanPhamBUS loaiSanPhamBUS)
        {
            ShopDataContext db = DataAccess.Database;
            LoaiSanPham cate = new LoaiSanPham()
            {
                MaLoai = loaiSanPhamBUS.MaLoai,
                TenLoai = loaiSanPhamBUS.TenLoai
            };
            db.LoaiSanPhams.InsertOnSubmit(cate);
            db.SubmitChanges();
            return cate.ID;
        }

        internal static void Update(LoaiSanPhamBUS loaiSanPhamBUS)
        {
            ShopDataContext db = DataAccess.Database;
            var cate = db.LoaiSanPhams.Single<LoaiSanPham>(p => p.ID == loaiSanPhamBUS.ID);
            cate.MaLoai = loaiSanPhamBUS.MaLoai;
            cate.TenLoai = loaiSanPhamBUS.TenLoai;
            db.SubmitChanges();
        }

        internal static void Delete(LoaiSanPhamBUS loaiSanPhamBUS)
        {
            ShopDataContext db = DataAccess.Database;
            var cate = db.LoaiSanPhams.Single<LoaiSanPham>(p => p.ID == loaiSanPhamBUS.ID);
            if (cate != null)
            {
                db.LoaiSanPhams.DeleteOnSubmit(cate);
                db.SubmitChanges();
            }
        }

        internal static string GetNameByID(int id)
        {
            ShopDataContext db = DataAccess.Database;
            var name = from p in db.LoaiSanPhams
                       where p.ID == id
                       select p.TenLoai;
            return name.FirstOrDefault<string>();
        }

        internal static LoaiSanPhamBUS GetByID(int ID)
        {
            ShopDataContext db = DataAccess.Database;
            var list = from p in db.LoaiSanPhams
                       where p.ID==ID
                       select new LoaiSanPhamBUS()
                       {
                           ID = p.ID,
                           MaLoai = p.MaLoai,
                           TenLoai = p.TenLoai
                       };
            return list.FirstOrDefault<LoaiSanPhamBUS>();
        }
    }
}
